package com.orhon.smartcampus.modules.base.fetcher;

import com.orhon.smartcampus.modules.base.dao.GradesDao;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Periods;
import com.orhon.smartcampus.modules.base.gqlModel.GQLGrades;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPeriod;
import com.orhon.smartcampus.modules.base.mm.GradeMapper;
import com.orhon.smartcampus.modules.base.mm.PeriodMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GradeFetcher implements DataFetcher {

    @Autowired
    GradesDao gradesDao;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        int id = Integer.parseInt(env.getArgument("id").toString());

        String hql = "from Grades where deleted_at = null and id = "+id;
        Grades item =  gradesDao.findId(hql, null);
        if(item!=null) {
        	
        	GQLPeriod gqlPeriod = null;
            if (env.getSelectionSet().contains("periods")){
                item.getPeriods();
                Periods periods = item.getPeriods();
                gqlPeriod = PeriodMapper.MAPPER.toPeriods(periods);
            }
            
            GQLGrades grades = GradeMapper.MAPPER.toGrade(item);
            grades.setPeriods(gqlPeriod);
            return grades;
        }
        return null;
    }


}
